/**
 * 格式化时间戳与当前时间的差值
 * @param {number} timestamp - 需比较的时间戳（毫秒级，如1699999999999）
 * @returns {string} 时间差值描述（如"3分钟前"、"2小时前"、"1天前"等）
 */
export function formatTimeDiff(timestamp) {
  // 1. 计算时间差值（毫秒），取绝对值避免未来时间戳异常
  const now = Date.now();
  const diffMs = Math.abs(now - timestamp);

  // 2. 定义时间单位换算（毫秒）
  const oneMinute = 60 * 1000;    // 1分钟 = 60000毫秒
  const oneHour = 60 * oneMinute; // 1小时 = 3600000毫秒
  const oneDay = 24 * oneHour;    // 1天 = 86400000毫秒
  const oneWeek = 7 * oneDay;     // 1周 = 604800000毫秒
  const oneMonth = 30 * oneDay;   // 1月（简化按30天算）= 2592000000毫秒
  const oneYear = 12 * oneMonth;  // 1年（简化按360天算）= 31104000000毫秒

  // 3. 按区间判断并返回对应描述
  if (diffMs < oneMinute) {
    return "1分钟前";
  } else if (diffMs < oneHour) {
    const minutes = Math.floor(diffMs / oneMinute);
    return `${minutes}分钟前`;
  } else if (diffMs < oneDay) {
    const hours = Math.floor(diffMs / oneHour);
    return `${hours}小时前`;
  } else if (diffMs < oneWeek) {
    const days = Math.floor(diffMs / oneDay);
    return `${days}天前`;
  } else if (diffMs < oneMonth) {
    const weeks = Math.floor(diffMs / oneWeek);
    return `${weeks}周前`;
  } else if (diffMs < oneYear) {
    const months = Math.floor(diffMs / oneMonth);
    return `${months}月前`;
  } else {
    const years = Math.floor(diffMs / oneYear);
    return `${years}年前`;
  }
}